package com.jiazhong.jdbc.spring.service;

import com.jiazhong.jdbc.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther:shuXin
 */
@Service
//只读
//@Transactional(readOnly = true)
//超时
//@Transactional(timeout = 3)
//回滚策略
//@Transactional(rollbackFor = ArithmeticException.class)
//@Transactional(noRollbackForClassName = "java.lang.ArithmeticException")
//事务隔离级别
//@Transactional(isolation = Isolation.READ_COMMITTED)
@Transactional(propagation = Propagation.REQUIRED)
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public void buyBook(Integer bookId, Integer userId) {
        /*try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //查询图书的价格
        Integer price =  userDao.getPriceBook(bookId);
        System.out.println(price);

        //更新图书的库存
        userDao.updateBookStock(bookId);
        //System.out.println(1/0);

        //更新用户的余额
        userDao.updateUserBalance(userId,price);
    }
}
